import { wrap, type EntityManager, WrappedEntity, expr, QueryOrder } from '@mikro-orm/core';
import { Factory, Seeder, faker } from '@mikro-orm/seeder';
import * as Mock from 'mockjs' 
import { Users } from '../entities/Users';
import { ChatRoomsFactory } from '../factorys/ChatRoomsFactory';
import { ulid } from 'ulid';
import * as dayjs from 'dayjs';
import { ChatRooms } from '../entities/ChatRooms';
import { knext } from 'knext';

const Random = Mock.Random

export class ChatSeeder extends Seeder {

  async run(em: EntityManager): Promise<void> {

    let noPage = 0;
    let size = 10;
    //let users = null
    let count = 0

    const seed = dayjs().valueOf();

    // do {
    //     users =  await em.find<Users>(Users,{},{offset:noPage * size,limit:size}) as any

    //     // const usersSet = users.map((user)=>wrap(user)) 

    //      for (let user of users) {
    //         const rooms = await new ChatRoomsFactory(em).each((room)=>{
    //           room.topic = ulid(seed);
    //         }).create(5,{
    //             creator:user,
    //             owner:user,
    //         })

    //         await em.persistAndFlush(rooms) 
    //     }

    //     em.find<ChatRooms>(ChatRooms,{creator:{$ne:user}})

    //     ++noPage
    //   } while (users.length >= size);


      noPage = 0

      do {
        let users =  await em.find<Users>(Users,{},{offset:noPage * size,limit:size}) as Users[]

         for (let user of users) {
            // const knex = knext({orm:{getMetadata:em.getMetadata.bind(em)},em})

            // const rooms =  await knex.fromORM(ChatRooms)
            // .where('creator_id','<>',user.id)
            // .orderByRaw('RAND()')
            // .paginate(noPage,size)
            // .populate()

            let raw : any = 'RAND()'
            const rooms = await em.find(ChatRooms,{creator:{$ne:user}},{orderBy:{[raw]:''}})

            //const roomsSet = rooms.map((room)=>wrap(room).init(room))

            user.roomsAsChatRoomMembers.add(rooms)
        }

        await em.persistAndFlush(users) 

        ++noPage
        count = users.length
      } while (count >= size);

    // const users = await new UsersFactory(em).each((user)=>{
    //   user.sources.set(
    //     new SourcesFactory(em).each((source)=>{
    //         source.tasks.set(
    //           new JobTasksFactory(em).each((task)=>{
    //               task.jobs.set(new JobFactory(em).each((job)=>{
                      
    //               }).make(4,{
    //                  task:task
    //               })) 
    //           }).make(10,{
    //             creator:user,
    //             owner:user,
    //           })
    //         )
    //     }).make(3,{
    //       creator:user,
    //       owner:user,
    //     })
    //   )
    // }).create(10,{
    //   sex: faker.name.sex(),
    // })

    // await em.persistAndFlush(users)

    // users.p

    // em.create(Users, {
    //   name: Random.cname(),
    // });
  }
}
